<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/1/12
 * Time: 20:10
 */
$s="Start Time :".date('Y-m-d H:i:s').PHP_EOL;
echo $s;
$workers=[];
$urls=[
    'https://www.baidu.com',
    'https://qq.com',
    'http://sina.com',
    'http://www.jd.com',
    'http://sina.com',
];
for($i=0;$i<count($urls);$i++){
    $process=new  swoole_process(function (swoole_process $pro) use($i,$urls){
        $re_content=sendHttp($urls[$i]);
        $pro->write($re_content);
//        echo $re_content.' process'.PHP_EOL; //php echo 会把结果返回到进程
    },true);
    $pid=$process->start();
    $workers[$pid]=$process;
}
//var_dump($workers);
//通过管道获取进程返回的内容
foreach ($workers as $worker){
    try{
         $r=$worker->read();
         swoole_async_writefile(__DIR__.'/curl.log',$r,function ($filename){},FILE_APPEND);
    }catch (\Exception $e){
        echo $e->getMessage();
    }
}
function sendHttp($url){
    sleep(1);
    return $url.' Success';
}
echo 'php script use time :' .date('Y-m-d H:i:s').PHP_EOL;
